package repository

import (
	"github.com/astaxie/beego/orm"
	"pq/dto"
	"pq/models"
)
type SubjectRepository struct{

}
func (sr *SubjectRepository) GetInfo(subjectDto dto.SubjectDto) models.PqCsTestItem{
	slave := models.GetSlaveDb()
	subjectEntiry := models.PqCsTestItem{Id:subjectDto.Get("id").(int)}
	slave.Read(&subjectEntiry)
	return subjectEntiry
}
func (sr *SubjectRepository) SetToResource(subjectDto dto.SubjectDto){
	master := models.GetMasterDb()
	//1.create new resource
	resource := new(models.PqCsTestResource)
	resource.Body = subjectDto.Get("body").(string)
	resource.PqCsTestLibraryId = subjectDto.Get("lib_id").(int)
	master.Insert(resource)
	//2.delete normal subject
	subject := &models.PqCsTestItem{Id:subjectDto.Get("id").(int)}
	master.Delete(subject)
}

func (sr *SubjectRepository)Delete(subjectDto dto.SubjectDto)(int64,error){
	master := models.GetMasterDb()
	subject := &models.PqCsTestItem{Id:subjectDto.Get("id").(int)}
	return master.Delete(subject)
}

func subjectGetItemsByNumbers(lib_id int,start int ,end int) []orm.Params{
	slave := models.GetSlaveDb()
	var subjects []orm.Params
	slave.QueryTable("pq_cs_test_item").Filter("pq_cs_test_library_id",lib_id).
		Filter("num__gte",start).Filter("num__lte",end).Values(&subjects)
	return subjects
}
func setToResource(subjectDto dto.SubjectDto){
	master := models.GetMasterDb()
	//1.create new resource
	resource := new(models.PqCsTestResource)
	resource.Body = subjectDto.Get("body").(string)
	resource.PqCsTestLibraryId = subjectDto.Get("lib_id").(int)
	master.Insert(resource)
	//2.delete normal subject
	subject := &models.PqCsTestItem{Id:subjectDto.Get("id").(int)}
	master.Delete(subject)
}
